package xyz.shuixiangyu.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import xyz.shuixiangyu.movie.Movie;
/**
 * This class runs to generate html 
 * @author ivan
 *
 */
public class HtmlProducer {
	private final String libPath;
	private final String headerTemplate;
	private final String bodyTemplate;
	private final String footerTemplate;

	public HtmlProducer(){
		libPath = System.getProperty("user.dir") + "/lib";
		headerTemplate = read(libPath + "/header");
		bodyTemplate = read(libPath + "/body");
		footerTemplate = read(libPath + "/footer");
	}

	/**
	 * produce a html page which contains several movies 
	 * @param movies
	 * @param nextPage , a href to next page,footer needs it
	 * @return
	 * @throws FileNotFoundException
	 */
	public String produce(Movie[] movies, String nextPage) {
		
		String html = "";
		StringBuffer body = new StringBuffer();
		for (Movie m : movies) {
			if(m!=null){
				body.append(produceBody(m));		
				System.out.println("add movie:"+m.getName());
			}
		}
		html = produceHeader() + body.toString() + produceFooter(nextPage);
		return html;
	}

	/**
	 * produce html body that contains several movies,the template is file lib/body
	 * @param m
	 * @return
	 * @throws FileNotFoundException
	 */
	public String produceBody(Movie m) {
		return bodyTemplate.replace("{{name}}", m.getName())
				.replace("{{year}}", m.getYear() + "")
				.replace("{{director}}", m.getDirector())
				.replace("{{ratings}}", m.getRatings() + "")
				.replace("{{photo}}", new File(m.getPhoto()).getName());

	}

	public String produceHeader()  {
		return headerTemplate;
	}

	public String produceFooter(String nextPage) {
		return footerTemplate.replace("{{nextPage}}", nextPage);
	}

	public int numOfMovies(String filePath) throws IOException {
		String content = read(filePath);
		if (content.contains("<article>")) {
			return content.split("<article>").length - 1;
		}
		return 0;
	}

	public String addMovie(Movie[] ms, String htmlFilePath){
		String html = read(htmlFilePath);
		if (html.contains("<div class=\"main\">")) {
			String[] headerAndOthers = html.split("<div class=\"main\">");
			StringBuffer newMovies=new StringBuffer();
			for(Movie m:ms){
				newMovies.append(produceBody(m));
				System.out.println("add movie:"+m.getName());
			}
			return headerAndOthers[0] + "<div class=\"main\">" + newMovies.toString()
					+ headerAndOthers[1];
		}
		return null;
	}
	
	public String updateLastPage(String htmlFilePath){
		String html = read(htmlFilePath);
		String[] othersAndFooter=html.split("<footer>");
		return othersAndFooter[0]+"<footer>"+produceFooter("1.html");
	}

	
	private String read(String filePath) {
		StringBuffer txtContent = new StringBuffer();
		BufferedReader br;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"utf-8"));
			String line=null;  
			while((line=br.readLine())!=null){
				txtContent.append(line+System.getProperty("line.separator"));
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return txtContent.toString();
	}
}
